package com.moli.iov.notify.remote.impl;

import com.alibaba.fastjson.JSONObject;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.notify.config.SmsProperties;
import com.moli.iov.notify.constants.Constant;
import com.moli.iov.notify.enums.SendStatusEnum;
import com.moli.iov.notify.model.dto.MailDto;
import com.moli.iov.notify.model.dto.SaveUserMailAndSmsDto;
import com.moli.iov.notify.model.dto.SmsDto;
import com.moli.iov.notify.model.dto.sms.SaveUserJsonDto;
import com.moli.iov.notify.remote.IMailRemoteService;
import com.moli.iov.notify.remote.ISmsRemoteService;
import com.moli.iov.notify.service.IMailService;
import com.moli.iov.notify.util.MailUtil;
import com.moli.iov.rest.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;

/**
 * <p>
 * 邮件业务
 * </p>
 *
 * @author zql
 * @since 2019/9/11 9:29
 */
@Slf4j
@Service(group = "${service.group}")
public class MailRemoteServiceImpl implements IMailRemoteService {

  @Autowired
  private MailUtil mailUtil;

  @Autowired
  private IMailService mailService;

  @Autowired
  private SmsProperties smsProperties;

  @Autowired
  private ISmsRemoteService smsRemoteService;

  /**
   * 添加用户发送 通知
   * @param mailAndSmsDto
   * @return
   */
  @Override
  public RestResponse sendMailAndSmsOfSaveUser(SaveUserMailAndSmsDto mailAndSmsDto) {
    try {

      checkSendMailAndSmsOfSaveUser(mailAndSmsDto);

      sendMail(assembleMailDto(mailAndSmsDto));
      smsRemoteService.sendSms(assembleSmsDtoOfSaveUser(mailAndSmsDto));
      return RestResponse.success();
    } catch (Exception e) {
      log.error("添加用户发送 通知异常", e);
      return RestResponse.failure("发送失败");
    }
  }

  /**
   * 发送邮件
   * @param mailDto
   * @return
   */
  @Async
  @Override
  public RestResponse sendMail(MailDto mailDto) {
    try {
      boolean ist = mailUtil.sendMail(mailDto);

      mailDto.setStatus(SendStatusEnum.SUCCESS.getFlag());
      if(!ist){
        mailDto.setStatus(SendStatusEnum.FAIL.getFlag());
      }

      mailService.saveNotifyMail(mailDto);

      return RestResponse.success();
    }catch(Exception e){
      log.error("发送邮件 异常", e);
      return RestResponse.failure("发送邮件失败");
    }
  }

  /**
   * 添加用户发送 通知参数检查
   * @param mailAndSmsDto
   */
  private void checkSendMailAndSmsOfSaveUser(SaveUserMailAndSmsDto mailAndSmsDto){
    if (StringUtils.isEmpty(mailAndSmsDto.getAccount())) {
      throw new BusinessException("用户名不能为空");
    }
    if (StringUtils.isEmpty(mailAndSmsDto.getPassword())) {
      throw new BusinessException("密码不能为空");
    }

    if(StringUtils.isEmpty(mailAndSmsDto.getEmail())){
      throw new BusinessException("邮箱不能为空");
    }

    if(StringUtils.isEmpty(mailAndSmsDto.getUserPhone())){
      throw new BusinessException("手机号不能为空");
    }
  }

  /**
   * 添加用户发送 组装email数据
   * @param mailAndSmsDto
   * @return
   */
  private MailDto assembleMailDto(SaveUserMailAndSmsDto mailAndSmsDto){
    MailDto mailDto = new MailDto();
    mailDto.setTo(mailAndSmsDto.getEmail());
    mailDto.setSubject(Constant.ADD_USER_MAIL_TITLE);
    String text = Constant.ADD_USER_MAIL_TEXT
        .replaceAll("@1", Constant.IOV_URL);
    text = text.replaceAll("@2", mailAndSmsDto.getAccount());
    text = text.replaceAll("@3", mailAndSmsDto.getPassword());
    mailDto.setText(text);

    return mailDto;
  }

  /**
   * 添加用户发送 组装短信数据
   * @param mailAndSmsDto
   * @return
   */
  private SmsDto assembleSmsDtoOfSaveUser(SaveUserMailAndSmsDto mailAndSmsDto){
    SmsDto smsDto = new SmsDto();
    smsDto.setTempleteCode(smsProperties.getSaveUserTempleteCode());
    smsDto.setMobile(mailAndSmsDto.getUserPhone());
    SaveUserJsonDto saveUserSmsDto = new SaveUserJsonDto(mailAndSmsDto.getEmail());
    smsDto.setJsonData(JSONObject.toJSONString(saveUserSmsDto));
    return smsDto;
  }
}
